Skip to content

[CALCITE-7818] Druid OR filters should not discard matching timestamp ranges - #5243

Open
bvolpato wants to merge 1 commit into
apache:mainfrom
bvolpato:bvolpato/fix-druid-or-intervals
Open

bvolpato wants to merge 1 commit into
apache:mainfrom
bvolpato:bvolpato/fix-druid-or-intervals

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7818

Changes Proposed

Druid interval extraction currently ignores OR branches it cannot represent as timestamp intervals. The filter rule then treats the remaining partial intervals as the complete condition and removes the filter, excluding valid rows.

Abort interval extraction if any OR branch is unrepresentable. This lets the existing filter path retain the full predicate. Add regressions for both operand orders, an empty-range branch, and planner behavior that preserves the original intervals and filter.

Reproduction

For a Druid table whose time column is timestamp, this predicate must retain a row dated 2021-01-15:

"timestamp" < TIMESTAMP '2020-01-01 00:00:00'
OR EXTRACT(DAY FROM "timestamp") = 15

The affected plan restricts intervals to dates before 2020 and drops the filter.

./gradlew :druid:test --tests 'org.apache.calcite.test.DruidDateRangeRulesTest' --tests 'org.apache.calcite.adapter.druid.DruidQueryFilterTest'

Validation

  • The mixed-OR utility and planner regressions fail against the baseline.
  • Direct JUnit execution against the compiled fix passes both complete changed classes: 12 tests, no failures or skips, with assertions enabled.
  • ./gradlew autostyleApply and git diff --check pass.
  • Focused runs used freshly compiled changed classes with cached supporting artifacts. Native Gradle test-class compilation on pristine main was blocked by java.io.IOException: No space left on device; the full Gradle build has not been validated locally.

Downsides

Mixed OR predicates may scan a wider interval and rely on Druid filtering. Narrowing the interval safely requires accounting for every disjunct.

@bvolpato
bvolpato force-pushed the bvolpato/fix-druid-or-intervals branch from 5a10772 to c1603bb Compare September 4, 2026 16:40
@sonarqubecloud

sonarqubecloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

@mihaibudiu

Copy link
Copy Markdown
Contributor

I am not planning to review these PRs until they are marked as non-draft and there is a JIRA issue.

@bvolpato bvolpato changed the title Druid OR filters should not discard matching timestamp ranges [CALCITE-7818] Druid OR filters should not discard matching timestamp ranges Sep 24, 2026
@bvolpato
bvolpato marked this pull request as ready for review September 24, 2026 01:59
intervals.addAll(extracted);
if (extracted == null) {
// Every disjunct must be represented to avoid excluding matching rows.
return null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has no javadoc, so it's hard to guess what "null" means.
It's hard even to guess what the returned result is supposed to be.
Maybe you can improve it a bit?

f.rexBuilder.makeCall(SqlStdOperatorTable.EXTRACT,
f.rexBuilder.makeFlag(TimeUnitRange.DAY), timestamp),
f.rexBuilder.makeExactLiteral(BigDecimal.valueOf(15))));
planner.setRoot(LogicalFilter.create(query, condition));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inferring the generated code is not easy, maybe you can help by adding some comments showing the equivalent SQL for the various expressions generated

final Fixture2 f = new Fixture2();
final RexNode range = f.lt(f.ts, f.timestampLiteral(2020, Calendar.JANUARY, 1));
final RexNode extract = f.eq(f.exDay, f.literal(15));
assertThat(DruidDateTimeUtils.createInterval(f.or(range, extract)), nullValue());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createInterval also does not say what a "null" result means, maybe you can improve its documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants